﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Lidgren.Network;
using Lidgren.Network.Xna;

namespace Networking.Packets
{
    class AttackPacket
    {
        private const int BitsPerElementQuaternion = 8;
        public const UInt64 TAG = 0;
        public AttackPacket() { }
        public AttackPacket(UInt64 guid, UInt64 spellId, UInt64 senderPlayerId, UInt64 targetPlayerId, Vector3 loc, Quaternion orientationQuaternion) 
            : this(guid, spellId,senderPlayerId, targetPlayerId, loc, orientationQuaternion, 0) { }
        public AttackPacket(UInt64 guid, UInt64 spellId, UInt64 senderPlayerId, UInt64 targetPlayerId, Vector3 loc)
            : this(guid, spellId, senderPlayerId, targetPlayerId, loc, new Quaternion(0, 0, 0, 0), 0) { }
        public AttackPacket(
            UInt64 guid, UInt64 spellId, UInt64 senderPlayerId, UInt64 targetPlayerId,
            Vector3 loc, Quaternion orientation, UInt64 senderUniqueId)
        {
            Guid = guid;
            Location = loc;
            SenderId = senderUniqueId;
            Orientation = orientation;
            SpellId = spellId;
            SenderPlayerId = senderPlayerId;
            TargetPlayerId = targetPlayerId;
        }
        public UInt64 SenderId { get; set; }
        public UInt64 Guid { get; private set; }
        public UInt64 SenderPlayerId { get; private set; }
        public UInt64 TargetPlayerId { get; private set; }
        public Vector3 Location { get; private set; }
        public Quaternion Orientation { get; private set; }
        public UInt64 SpellId { get;private set; }

        public static void BuildOutputMessage(ref NetOutgoingMessage msg, AttackPacket pack)
        {
            msg.Write(TAG);
            msg.Write(pack.Guid);
            msg.Write(pack.SpellId);
            msg.Write(pack.SenderPlayerId);
            msg.Write(pack.TargetPlayerId);
            XNAExtensions.Write(msg, pack.Location);
            XNAExtensions.WriteRotation(msg, pack.Orientation, BitsPerElementQuaternion);
        }
        /// <summary>
        /// Parses the message and populates the packet with its information. This assumes the TAG has already been stripped from the packet
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="pack"></param>
        public static void ParseMessage(NetIncomingMessage msg, ref AttackPacket pack)
        {
            pack.Guid = (UInt64)msg.ReadInt64();
            pack.SpellId = (UInt64)msg.ReadInt64();
            pack.SenderPlayerId = (UInt64)msg.ReadInt64();
            pack.TargetPlayerId = (UInt64)msg.ReadInt64();
            pack.Location = XNAExtensions.ReadVector3(msg);
            pack.Orientation = XNAExtensions.ReadRotation(msg, BitsPerElementQuaternion);
        }

    }
}
